<template>
  <el-dialog :title="titleRoad" append-to-body :close-on-click-modal="false" @close="closeDialog()" v-model="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="110px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="请选择停车场" prop="parkId">
            <el-select v-model="dataForm.parkId" class="m-2" placeholder="请选择停车场" @change="getParkId">
              <el-option v-for="item in parkNames" :key="item.id" :label="item.parkName" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="请选择区域" prop="regionId">
            <el-select v-model="dataForm.regionId" class="m-2" placeholder="请选择区域">
              <el-option v-for="item in regionNames" :key="item.id" :label="item.regionName" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="车位号" prop="lotNo">
        <el-input v-model="dataForm.lotNo" placeholder="车位号"></el-input>
      </el-form-item>

      <!-- 
      <el-form-item label="车位状态 0 未占用 1 已锁定待付款 2 租赁中 3 路边车位已占用" prop="status">
        <el-input v-model="dataForm.status" placeholder="车位状态 0 未占用 1 已锁定待付款 2 租赁中 3 路边车位已占用"></el-input>
      </el-form-item> -->
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { getObj, addObj, putObj } from '@/api/roadStop/regionlotroad'

export default {
  props: {
    parkName: { type: Object },
    regionName: { type: Object },
    getDataListRoad: { type: Function },
  },
  data () {
    return {
      visible: false,
      canSubmit: false,
      dataForm: {
        status: "0"
      },
      dataRule: {
        lotNo: [
          { required: true, message: '车位号不能为空', trigger: 'blur' }
        ],

        parkId: [
          { required: true, message: '停车场id不能为空', trigger: 'blur' }
        ],

        regionId: [
          { required: true, message: '区域id不能为空', trigger: 'blur' }
        ],

        isSync: [
          { required: true, message: '是否已下发 0-未下发 1-已下发不能为空', trigger: 'blur' }
        ],

        status: [
          { required: true, message: '车位状态 0 未占用 1 已锁定待付款 2 租赁中 3 路边车位已占用不能为空', trigger: 'blur' }
        ],

      },
      parkNames: [],
      regionNames: [],
      titleRoad: null
    }
  },
  watch: {
    regionName: {
      handler (newOld) {
        this.regionNames = newOld
      },
      immediate: true,
    }
  },
  methods: {
    getParkId (val) {
      this.dataForm.regionId = undefined
      this.$emit('fatherMethod', val)
    },
    init (id) {
      if (id == undefined) {
        this.titleRoad = '新增'
      } else {
        this.titleRoad = '修改'
      }
      this.parkNames = this.parkName
      this.regionNames = this.regionName
      this.visible = true;
      this.canSubmit = true;
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (id) {
          getObj(id).then(response => {
            this.dataForm = response.data.data
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          if (this.dataForm.id) {
            putObj(this.dataForm).then(data => {
              this.$notify.success('修改成功')
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.canSubmit = true;
            });
          } else {
            addObj(this.dataForm).then(data => {
              this.$notify.success('添加成功')
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.canSubmit = true;
            });
          }
        }
      })
    },
    //重置表单
    closeDialog () {
      this.$refs["dataForm"].resetFields()
    }
  }
}
</script>
